package com.halflife.comeElectricity.member.rider;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ActivateLogAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ActivateLogRepository activateLogRepository;

    @RequestMapping("/activateLog/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        int totalCount = activateLogRepository.selectCount(queryParam);
        List<ActivateLog> list = activateLogRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "activateLog/list";
    }

    @RequestMapping("/activateLog/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        ActivateLog activateLog = new ActivateLog();
        if(id!=null){
            activateLog = activateLogRepository.selectByPrimaryKey(id);
        }
        model.addAttribute("activateLog",activateLog);
        return "activateLog/operate";
    }

    @PostMapping("/activateLog/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(ActivateLog activateLog, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(activateLog.getId()!=null){
                activateLogRepository.updateByPrimaryKey(activateLog);
            }else {
                activateLog.init();
                Integer a = activateLogRepository.insert(activateLog);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/activateLog/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            ActivateLog activateLog = new ActivateLog();
            activateLog.setId(id);
            activateLog.setValid(Boolean.FALSE);
            activateLogRepository.updateByPrimaryKeySelective(activateLog);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
